<?php 

function _getDefaultPlannerId($db) {
	$user = JFactory::getUser();
	
	$query = $db->getQuery(true);	
	$query->select('planner_id');
	$query->from('#__mariageplanner_user_lnk');
	$query->where('uid=' . (int)$user->id);
	$db->setQuery($query);
	$data = $db->loadObject();

	if ($error = $db->getErrorMsg()) {
		throw new JException($error);
	}

	if(empty($data)) {				
		$query = $db->getQuery(true);
		$query->insert('#__mariageplanner');
		$query->values('');
		$db->setQuery($query);
	
		if (!$db->query()) {
			JError::raiseError(500, $db->getErrorMsg());
		}
		$pid = $db->insertid();		
	
		$query = $db->getQuery(true);
		$query->insert('#__mariageplanner_user_lnk');
		$query->columns(array($db->quoteName('planner_id'), $db->quoteName('uid')));
		$query->values( (int)$pid . ',' . (int)$user->id );

		$db->setQuery($query);
		if (!$db->query()) {
			JError::raiseError(500, $db->getErrorMsg());
		}
		
		return $pid;
	}
	
	return $data->planner_id;
}

?>